/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.uch.biblioteca.managedBeans;

import java.io.EOFException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;
import net.uch.biblioteca.hibernateSpringDao.HSBibCatalogoDetalleDAO;
import net.uch.biblioteca.hibernateSpringDao.HSBibLectorDAO;
import net.uch.biblioteca.hibernateSpringDao.HSBibPrestamoDAO;
import net.uch.biblioteca.mapping.BibCatalogoDetalle;
import net.uch.biblioteca.mapping.BibPrestamo;
import net.uch.biblioteca.mapping.BibUsuario;
import net.uch.util.CommonDAO;
import org.eclipse.jdt.internal.compiler.ast.CombinedBinaryExpression;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import net.uch.biblioteca.hibernateSpringDao.HSBibEjemplarDAO;
import net.uch.util.Reporte;
import java.util.HashMap;
import java.io.OutputStream;

public class mbPrestamo {

    private String sLec_codigo = "";
    private int iPreId = 0;
    private String sDocTit = "";
    private String sPreTip = "";
    private Date dFecRes;
    private Date repFec_ini;
    private Date repFec_fin;
    private Date dFecDev;
    private List<mbPrestamo> prestamos = new ArrayList<mbPrestamo>();
    private String oncomplete;
    BibUsuario usu;
    private String sTipoReporte = "";
    private String valorRep = "";
    private String tituloReporte = "";
    private String obsPre = "";
    
    // CONSTRUCTORES
    public mbPrestamo () {
    }

    public mbPrestamo ( int a ) {
    }

    

    //SETTER Y GUETTER
    public BibUsuario getUsu () {
        return usu;
    }

    public void setUsu ( BibUsuario usu ) {
        this.usu = usu;
    }

    public String getObsPre () {
        return obsPre;
    }

    public void setObsPre ( String obsPre ) {
        this.obsPre = obsPre;
    }
    
    public String getTituloReporte () {
        return tituloReporte;
    }

    public void setTituloReporte ( String tituloReporte ) {
        this.tituloReporte = tituloReporte;
    }

    public String getValorRep () {
        return valorRep;
    }

    public void setValorRep ( String valorRep ) {
        this.valorRep = valorRep;
    }

    public String getsTipoReporte () {
        return sTipoReporte;
    }

    public void setsTipoReporte ( String sTipoReporte ) {
        this.sTipoReporte = sTipoReporte;
    }

    public Date getRepFec_fin () {
        return repFec_fin;
    }

    public void setRepFec_fin ( Date repFec_fin ) {
        this.repFec_fin = repFec_fin;
    }

    public Date getRepFec_ini () {
        return repFec_ini;
    }

    public void setRepFec_ini ( Date repFec_ini ) {
        this.repFec_ini = repFec_ini;
    }

    public String getOncomplete () {
        return oncomplete;
    }

    public void setOncomplete ( String oncomplete ) {
        this.oncomplete = oncomplete;
    }

    public Date getdFecDev () {
        return dFecDev;
    }

    public void setdFecDev ( Date dFecDev ) {
        this.dFecDev = dFecDev;
    }

    public Date getdFecRes () {
        return dFecRes;
    }

    public void setdFecRes ( Date dFecRes ) {
        this.dFecRes = dFecRes;
    }

    public int getiPreId () {
        return iPreId;
    }

    public void setiPreId ( int iPreId ) {
        this.iPreId = iPreId;
    }

    public String getsDocTit () {
        return sDocTit;
    }

    public void setsDocTit ( String sDocTit ) {
        this.sDocTit = sDocTit;
    }

    public String getsPreTip () {
        return sPreTip;
    }

    public void setsPreTip ( String sPreTip ) {
        this.sPreTip = sPreTip;
    }

    public List<mbPrestamo> getPrestamos () {
        return prestamos;
    }

    public void setPrestamos ( List<mbPrestamo> prestamos ) {
        this.prestamos = prestamos;
    }

    public String getsLec_codigo () {
        return sLec_codigo;
    }

    public void setsLec_codigo ( String sLec_codigo ) {
        this.sLec_codigo = sLec_codigo;
    }

    public String ListarReservasxLector () throws Exception {

        int Lec_id = 0;

        System.out.println( "codigo: " + this.sLec_codigo );

        HSBibLectorDAO daoLec = CommonDAO.getBibLectorDAO();
        HSBibCatalogoDetalleDAO daodet = CommonDAO.getBibCatalogoDetalleDAO();
        HSBibPrestamoDAO daoPre = CommonDAO.getBibPrestamoDocDAO();
        List lstReservas = new ArrayList();
        int idCatDoc = 0;
        List<BibCatalogoDetalle> lstdet;

        if ( daoLec.lectorxCodigo( this.sLec_codigo ) != null ) {
            Lec_id = daoLec.lectorxCodigo( this.sLec_codigo ).getLecId();
            lstReservas = daoPre.traerReservasxLector( Lec_id );
        }



        this.prestamos = new ArrayList<mbPrestamo>();
        String Doctit2 = "";
        String Doctit = "";

        for ( int i = 0; i < lstReservas.size(); i++ ) {

            mbPrestamo prestamo = new mbPrestamo( 0 );

            if ( ((BibPrestamo) lstReservas.get( i )).getPreTip().equals( "002001" ) ) {
                prestamo.setsPreTip( "SALA" );
            } else {
                prestamo.setsPreTip( "DOMICILIO" );
            }

            idCatDoc = daodet.traerCatDocumento( ((BibPrestamo) lstReservas.get( i )).getBibEjemplar().getCatEje() ).getCatId();
            lstdet = daodet.traerTituloDoc( idCatDoc );

            Doctit2 = lstdet.get( 0 ).getMarCont().substring( 2, lstdet.get( 0 ).getMarCont().length() );

            String[] detalleseparado = Doctit2.split( "\\^" );
            for ( int u = 0; u < detalleseparado.length; u++ ) {
                if ( detalleseparado[u].length() == 0 ) {
                    Doctit += " ";
                } else {
                    if ( u == 0 ) {
                        Doctit = detalleseparado[u];
                    } else {
                        Doctit += "/" + detalleseparado[u].substring( 1, detalleseparado[u].length() );
                    }

                }

            }

            prestamo.setiPreId( ((BibPrestamo) lstReservas.get( i )).getPreId() );
            prestamo.setsDocTit( Doctit );
            prestamo.setdFecRes( ((BibPrestamo) lstReservas.get( i )).getPreFecActual() );
            prestamo.setdFecDev( ((BibPrestamo) lstReservas.get( i )).getPreFecProdev() );

            prestamos.add( prestamo );
        }
        return "";
    }
    
    public void PrestarDoc(ActionEvent event) throws Exception {

        this.setObsPre("");

        int id_update = Integer.parseInt(((UIParameter) event.getComponent().findComponent("id_update")).getValue().toString());
        this.setiPreId(id_update);

        this.setOncomplete("Richfaces.showModalPanel('mpPrestamo')");
    }

    public void aprobarPrestamo ( ActionEvent event ) throws Exception {

        HSBibPrestamoDAO daopre = CommonDAO.getBibPrestamoDocDAO();
        this.setOncomplete( "" );
        int id_update = Integer.parseInt( ((UIParameter) event.getComponent().findComponent( "id_update" )).getValue().toString() );

        ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
        HttpSession session = (HttpSession) context.getSession( true );
        usu = (BibUsuario) session.getAttribute( "usuario" );
        System.out.println( "usuId: " + usu.getUsuId() );
        daopre.AprobarPrestamo( id_update, usu.getUsuId(),this.getObsPre() );

        this.setOncomplete( "javascript:alert('Prestamo Satisfactorio.');" );

    }

    public void verificarEliminacion ( ActionEvent event ) {

        int id_delete = Integer.parseInt( ((UIParameter) event.getComponent().findComponent( "id_delete" )).getValue().toString() );
        this.setiPreId( id_delete );
        this.setOncomplete( "javascript:Richfaces.showModalPanel('mpEliminar');" );



    }

    public void cancelarReserva ( ActionEvent event ) throws Exception {
        this.setOncomplete( "" );
        HSBibPrestamoDAO daopre = CommonDAO.getBibPrestamoDocDAO();
        HSBibEjemplarDAO daoeje = CommonDAO.getBibEjemplarDAO();

        daopre.cancelarReserva( iPreId );// cancelar prestamo
        daoeje.actualizarEjemplar( daopre.buscarPrestamoPorId( iPreId ).getBibEjemplar().getEjeId(), "0" );
        this.setOncomplete( "javascript:alert('La reserva se cancelo correctamente')" );
    }

    public void ImprimirFicha ( ActionEvent event ) throws Exception {
        this.setTituloReporte( "Reporte General" );
        this.setValorRep( "ficha" );
//           this.setB_sec_id( ((UIParameter) event.getComponent().findComponent( "p_sec_id" )).getValue().toString() );
    }

    public void cargarReporte ( OutputStream out, Object data ) throws IOException, Exception, EOFException {
        Reporte reporte = new Reporte();
        FacesContext context = FacesContext.getCurrentInstance();
        HashMap parametros = new HashMap();

        parametros.put( "fec_ini", this.repFec_ini );
        parametros.put( "fec_fin", this.repFec_fin );

        if ( "006001".equals( this.sTipoReporte ) ) { //REPORTE TIPO PRESTAMO
            reporte.cargarReporte( out, data, parametros, "rep_prestamo_tip_pre.jasper" );
        }

        if ( "006002".equals( this.sTipoReporte ) ) { // REPORTE TIPO DOCUMENTO
            reporte.cargarReporte( out, data, parametros, "rep_prestamo_tip_doc.jasper" );
        }

        if ( "006003".equals( this.sTipoReporte ) ) { // REPORTE TIPO LECTOR
            reporte.cargarReporte( out, data, parametros, "rep_prestamo_tip_lec.jasper" );
        }

    }
}
